// "use strict";

import Vue from 'vue';
import axios from "axios";

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  baseURL: process.env.baseURL || process.env.apiUrl || "http://localhost:3000",
  timeout: 60 * 1000, // Timeout
};

const api = axios.create(config);

api.interceptors.request.use(
  function(config) {
    if(localStorage.token){
      config.headers.Authorization = "Bearer " + localStorage.token
    }
    if (sessionStorage.token) {
      config.headers.Authorization = "Bearer " + sessionStorage.token
    }
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
api.interceptors.response.use(
  function(response) {
    // Do something with response data
    if(response.data.code == 1){
      Vue.$message({
        message: response.data.message,
        type: 'warning',
        showClose: true,
      });
      
    }
    return response.data
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

Plugin.install = function(Vue, options) {
  Vue.axios = api;
  window.axios = api;
  Object.defineProperties(Vue.prototype, {
    api: {
      get() {
        return api;
      }
    },
    $api: {
      get() {
        return api;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
